package com.example.a0510spinner0511listview;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class SpinnerActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {

    private TextView mTextView2;
    private Spinner mSpinner;
    private String[] strWeek = {"周一元气十足", "周二元气仍在", "周三还剩一点", "周四加油吧", "周五再加油", "周六周末啦", "周日挺内疚的"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_spinner);
        initView();
        initData();
    }

    private void initData() {
        // 初始化ArrayAdapter，泛型现在为String类型
        ArrayAdapter<String> mArrayAdapter = new ArrayAdapter<>(
                this, android.R.layout.simple_spinner_item, strWeek);
        mSpinner.setPrompt("请选择日期"); // 设置标题
        mSpinner.setAdapter(mArrayAdapter); // 组件与适配器绑定
        mSpinner.setSelection(0); // 默认选中第一行
    }

    private void initView() {
        mTextView2 = (TextView) findViewById(R.id.txtView2);
        mSpinner = (Spinner) findViewById(R.id.spinner);
        mSpinner.setOnItemSelectedListener(this);
    }

    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
        String strMsg = String.format("选择了第%d个选项:%s", i + 1, strWeek[i]);
        Toast.makeText(this, strMsg, Toast.LENGTH_LONG).show();
    }

    @Override
    public void onNothingSelected(AdapterView<?> adapterView) {
        // 当没有选中时的处理
    }
}